--
-- Created by IntelliJ IDEA.
-- User: rxx78
-- Date: 12. 11. 15
-- Time: 오후 11:06
-- To change this template use File | Settings | File Templates.
--

--
-- Created by IntelliJ IDEA.
-- User: rxx78
-- Date: 12. 11. 15
-- Time: 오후 11:00
-- To change this template use File | Settings | File Templates.
--
local scrollProcessor = require( "scrollProcessor" )

local theme = {}

function theme.create()
	local theme = { layer = display.newGroup() }
	theme.lastupdateTime = 0
	theme.childList = {}
	theme.camera = { x = 0, y = 0, width = display.contentWidth, height = display.contentHeight }
	function theme.update() end
	function theme:addChild( child )
		self.childList[#self.childList + 1] = child
	end

	function theme.frontProcess()
		theme.scroller = scrollProcessor.createScrollProcessor( theme.layer, "front.png" )
		theme.scroller.view.x = ( theme.scroller.view.width / 2 )
		theme.scroller.view.y = display.contentHeight - ( theme.scroller.view.height / 2 )

		theme:addChild( theme.scroller )

		theme.update = theme.centerProcess

		return true
	end

	function theme:needNewBlock()
		if theme.scroller.view.contentBounds.xMax < ( theme.camera.x + theme.camera.width ) then
			print(theme.scroller.view)
			print("1 : " .. theme.scroller.view.contentBounds.xMax)
			print("2 : " .. theme.camera.x )
			print("3 : " .. theme.camera.width )
			print("4 : " .. ( theme.camera.x + theme.camera.width ))
			return true
		end

		return false
		--return theme.scroller.view.contentBounds.xMax < display.contentWidth
	end

	function theme.centerProcess()
		if theme:needNewBlock() then
			local scroller = scrollProcessor.createScrollProcessor( theme.layer, "center.png" )
			print(theme.scroller.view)
			print("=======1 : " .. theme.scroller.view.contentBounds.xMax)
			print("=======2 : " .. theme.camera.x )
			print("=======3 : " .. theme.camera.width )
			print("=======4 : " .. ( theme.camera.x + theme.camera.width ))
			print("new Block Pos X : " .. theme.scroller.view.contentBounds.xMax + ( scroller.view.width / 2 ) )
			scroller.view.x = theme.scroller.view.contentBounds.xMax + ( scroller.view.width / 2 )
			scroller.view.y = display.contentHeight - ( scroller.view.height / 2 )

			theme.scroller = scroller
			theme:addChild( theme.scroller )

			return true
		end

		return false
	end

	function theme.enterFrame()
		theme.layer.x = 0

		local updateResult = true
		while updateResult do
			updateResult = theme.update()
		end

		local currentIndex = 1
		while currentIndex <= #theme.childList do
			if theme.childList[currentIndex]:isInvalid( theme.camera ) then
				theme.childList[currentIndex]:destory()
				table.remove( theme.childList, currentIndex )
			else
				currentIndex = currentIndex + 1;
			end
		end
		print( "childList : " .. #theme.childList )

		theme.layer.x = -theme.camera.x
	end

	function theme:play()
		self.lastupdateTime = system:getTimer()
		Runtime:addEventListener( "enterFrame", self )
	end

	theme.update = theme.frontProcess

	return theme
end

return theme
